package processingdata.density;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import processing.core.PVector;
import Control.Airport;
import Control.PopDensity;

public class DensitySerializer {

	static ArrayList<PopDensity> densities = new ArrayList<PopDensity>();

	public static void main(String args[]) throws IOException {

		try {
			BufferedReader inputReader = new BufferedReader(new FileReader(
					new File("data/density.data")));

			while (inputReader.ready()) {
				String inputReaderLine = inputReader.readLine();
				String inputReaderLineParts[] = inputReaderLine.split("\t");
				if(inputReaderLineParts.length==6)
				{
					String densityId = inputReaderLineParts[0].trim();
					String state = inputReaderLineParts[1].trim();
					String city = inputReaderLineParts[2].trim();
					float popDensity = Float.parseFloat(inputReaderLineParts[3].trim());
					float latitude = Float.parseFloat(inputReaderLineParts[4]);
					float longitude = Float.parseFloat(inputReaderLineParts[5]);

					System.out.println(inputReaderLine);
					if (latitude == 0 && longitude == 0) {
						continue;
					}
					PVector coordinates = new PVector(latitude, longitude);

					PopDensity density = new PopDensity();
					density.setCoordinates(coordinates);
					density.setCity(city);
					density.setState(state);
					density.setPopulationDensity(popDensity);
					density.setDensityId(densityId);
					densities.add(density);	
				}
				
			}

			serializeDensity();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	static void serializeDensity() throws FileNotFoundException, IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				new File("data/density.ser")));
		oos.writeObject(densities);
		oos.close();
	}

}
